package com.itmiaojie.service;

import com.alibaba.dubbo.config.annotation.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itmiaojie.dao.CheckGroupDao;
import com.itmiaojie.entity.PageResult;
import com.itmiaojie.pojo.CheckGroup;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Override
    public void add(CheckGroup checkGroup, Integer[] ids) {
        checkGroupDao.add(checkGroup);
        Integer checkGroupId = checkGroup.getId();
       setCheckGroupAndCheckItem(checkGroupId,ids);
    }

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        if (queryString != null) queryString = "%"+queryString+"%";
        Page<CheckGroup> pages = checkGroupDao.selectByCondition(queryString);
        System.out.println(pages);
        List<CheckGroup> result = pages.getResult();
        for (CheckGroup checkGroup : result) {
            System.out.println(checkGroup);
        }

        return new PageResult(pages.getTotal(),pages.getResult());
        //return "{\"rows\":[{\"age\":\"0-100\",\"attention\":\"无\",\"code\":\"0002\",\"id\":29,\"name\":\"体重\",\"price\":5.0,\"remark\":\"体重\",\"sex\":\"0\",\"type\":\"1\"},{\"age\":\"0-100\",\"attention\":\"无\",\"code\":\"0003\",\"id\":30,\"name\":\"体重指数\",\"price\":5.0,\"remark\":\"体重指数\",\"sex\":\"0\",\"type\":\"1\"},{\"age\":\"0-100\",\"attention\":\"无\",\"code\":\"0004\",\"id\":31,\"name\":\"收缩压\",\"price\":5.0,\"remark\":\"收缩压\",\"sex\":\"0\",\"type\":\"1\"},{\"age\":\"0-100\",\"attention\":\"无\",\"code\":\"0005\",\"id\":32,\"name\":\"舒张压\",\"price\":5.0,\"remark\":\"舒张压\",\"sex\":\"0\",\"type\":\"1\"},{\"age\":\"0-100\",\"attention\":\"无\",\"code\":\"0006\",\"id\":33,\"name\":\"裸视力（右）\",\"price\":5.0,\"remark\":\"裸视力（右）\",\"sex\":\"0\",\"type\":\"1\"},{\"age\":\"0-100\",\"attention\":\"无\",\"code\":\"0007\",\"id\":34,\"name\":\"裸视力（左）\",\"price\":5.0,\"remark\":\"裸视力（左）\",\"sex\":\"0\",\"type\":\"1\"},{\"age\":\"0-100\",\"attention\":\"无\",\"code\":\"0008\",\"id\":35,\"name\":\"矫正视力（右）\",\"price\":5.0,\"remark\":\"矫正视力（右）\",\"sex\":\"0\",\"type\":\"1\"},{\"age\":\"0-100\",\"attention\":\"无\",\"code\":\"0009\",\"id\":36,\"name\":\"矫正视力（左）\",\"price\":5.0,\"remark\":\"矫正视力（左）\",\"sex\":\"0\",\"type\":\"1\"},{\"age\":\"0-100\",\"attention\":\"无\",\"code\":\"0010\",\"id\":37,\"name\":\"色觉\",\"price\":5.0,\"remark\":\"色觉\",\"sex\":\"0\",\"type\":\"1\"},{\"age\":\"0-100\",\"attention\":\"无\",\"code\":\"0011\",\"id\":38,\"name\":\"白细胞计数\",\"price\":10.0,\"remark\":\"白细胞计数\",\"sex\":\"0\",\"type\":\"2\"}],\"total\":66}";
    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupDao.selectById(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.selectAll();
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkGroupDao.findCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //根据检查组id删除中间表数据（清理原有关联关系）
        checkGroupDao.deleteAssociation(checkGroup.getId());
        //向中间表(t_checkgroup_checkitem)插入数据（建立检查组和检查项关联关系）
        setCheckGroupAndCheckItem(checkGroup.getId(),checkitemIds);
        //更新检查组基本信息
        checkGroupDao.edit(checkGroup);
    }

    @Override
    public void delete(Integer id) {
        checkGroupDao.delete(id);
    }


    //设置检查组合和检查项的关联关系
    public void setCheckGroupAndCheckItem(Integer checkGroupId,Integer[] checkitemIds){
        if(checkitemIds != null && checkitemIds.length > 0){
            for (Integer checkitemId : checkitemIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("checkgroup_id",checkGroupId);
                map.put("checkitem_id",checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }
}
